package cn.net.shuxing.yifu.common.service;

import cn.net.shuxing.yifu.common.bean.DealHistory;
import cn.net.shuxing.yifu.common.bean.ShareholderBuyGrantHistory;
import cn.net.shuxing.yifu.common.bean.ShareholderCoinGrantHistory;
import cn.net.shuxing.yifu.common.constant.Type;
import cn.net.shuxing.yifu.common.mapper.ShareholderCoinGrantHistoryMapper;
import cn.net.shuxing.yifu.common.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.net.shuxing.yifu.common.util.StringUtils;
import redis.clients.jedis.Jedis;
import tk.mybatis.mapper.entity.Example;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

@Service
public class ShareholderCoinGrantHistoryService extends HistoryService<ShareholderCoinGrantHistory, ShareholderCoinGrantHistoryMapper> {

    @Autowired
    private UserDealHistoryService userDealHistoryService;

    @Autowired
    private JedisUtil jedisUtil;

    public List<ShareholderCoinGrantHistory> getsByCoinTypeIdAndType(Integer coinTypeId, Integer type) {
        CommonExample example = new CommonExample(ShareholderCoinGrantHistory.class);
        example.equals("coinTypeId", coinTypeId);
        example.equals("type", type);
        return getsByExample(example);
    }
    public List<ShareholderCoinGrantHistory> getsByCoinTypeIdAndType(Integer coinTypeId, Integer type,Integer userId) {
        CommonExample example = new CommonExample(ShareholderCoinGrantHistory.class);
        example.equals("coinTypeId", coinTypeId);
        example.equals("type", type);
        example.equals("userId", userId);
        return getsByExample(example);
    }

    public List<ShareholderCoinGrantHistory> getsByCoinTypeId(Integer coinTypeId) {
        CommonExample example = new CommonExample(ShareholderCoinGrantHistory.class);
        example.equals("coinTypeId", coinTypeId);
        return getsByExampleWithoutComplete(example);
    }

    public void insert(Integer coinTypeId, Integer shareholderId, BigDecimal count, String orderKey) {
        //Jedis //jedis = jedisUtil.getJedis();
        String s = jedisUtil.get(RedisKeyCreator.openPrice(coinTypeId));
        //jedisUtil.close();
        BigDecimal oldPrice = new BigDecimal(0);
        if (StringUtils.hasText(s))
            oldPrice = new BigDecimal(s);
        insert(coinTypeId, shareholderId, count, 0, oldPrice, orderKey);
    }

    public void insert(Integer coinTypeId, Integer userId, BigDecimal count, Integer type, BigDecimal oldPrice, String orderKey) {
        ShareholderCoinGrantHistory shareholderCoinGrantHistory = new ShareholderCoinGrantHistory();
        shareholderCoinGrantHistory.setCoinTypeId(coinTypeId);
        shareholderCoinGrantHistory.setUserId(userId);
        shareholderCoinGrantHistory.setCount(count);
        shareholderCoinGrantHistory.setLeftCount(count);
        shareholderCoinGrantHistory.setType(type);
        shareholderCoinGrantHistory.setOldPrice(oldPrice);
        shareholderCoinGrantHistory.setOrderKey(orderKey);
        insert(shareholderCoinGrantHistory);
    }

    public List<ShareholderCoinGrantHistory> getTodayByCoinTypeIdAndType(Integer coinTypeId, Integer type) {
        TimeExample example = new TimeExample(ShareholderCoinGrantHistory.class);
        example.equals("coinTypeId", coinTypeId);
        example.equals("type", type);
        Example.Criteria criteria = example.getCriteria();
        criteria.andGreaterThan("createTime", LocalDateTimeUtil.getToday());
        example.and(criteria);
        return getsByExample(example);
    }

    public BigDecimal getTotalSleepCoinCount() {
        CommonExample example = new CommonExample(ShareholderCoinGrantHistory.class);
        example.equals("type", 1);
        List<ShareholderCoinGrantHistory> shareholderCoinGrantHistoryList = getsByExample(example);
        BigDecimal count = new BigDecimal(0);
        for (ShareholderCoinGrantHistory shareholderCoinGrantHistory : shareholderCoinGrantHistoryList) {
            count = count.add(shareholderCoinGrantHistory.getCount());
        }
        return count;
    }


    @Override
    protected void complete(ShareholderCoinGrantHistory bean) {
        if (bean == null)
            return;
        Integer coinTypeId = bean.getCoinTypeId();
        //Jedis //jedis = jedisUtil.getJedis();
        String s = jedisUtil.get(RedisKeyCreator.openPrice(coinTypeId));
        //jedisUtil.close();
        if (StringUtils.hasText(s))
            bean.setCurrentPrice(new BigDecimal(s));
        else
            bean.setCurrentPrice(new BigDecimal(0));
    }

    public List<ShareholderCoinGrantHistory> getsByUserIdAndCoinType(Integer userId, Integer coinTypeId) {
        TimeExample example = new TimeExample(ShareholderCoinGrantHistory.class);
        example.equals("coinTypeId", coinTypeId);
        example.equals("userId", userId);
        example.equals("type", Type.ALIVECOIN);
        example.setOrderByClause("id DESC");
        List<ShareholderCoinGrantHistory> shareholderCoinGrantHistoryList = getsByExample(example);
        if (shareholderCoinGrantHistoryList != null && shareholderCoinGrantHistoryList.size() > 7) {
            while (shareholderCoinGrantHistoryList.size() > 7) {
                ShareholderCoinGrantHistory shareholderCoinGrantHistory = shareholderCoinGrantHistoryList.get(shareholderCoinGrantHistoryList.size() - 1);
                Integer id = shareholderCoinGrantHistory.getId();
                delete(id);
                shareholderCoinGrantHistoryList.remove(shareholderCoinGrantHistory);
            }
        }
        if (shareholderCoinGrantHistoryList != null && !shareholderCoinGrantHistoryList.isEmpty()) {
            Iterator<ShareholderCoinGrantHistory> iterator = shareholderCoinGrantHistoryList.iterator();
            while (iterator.hasNext()) {
                ShareholderCoinGrantHistory shareholderCoinGrantHistory = iterator.next();
                if (shareholderCoinGrantHistory.getLeftCount().compareTo(new BigDecimal(0)) <= 0) {
                    iterator.remove();
                }
            }
        }

        return shareholderCoinGrantHistoryList;
    }
    //    getsByUserIdAndCoinTypeWithoutRemove
    public List<ShareholderCoinGrantHistory> getsByUserIdAndCoinTypeWithoutRemove(Integer userId, Integer coinTypeId) {
        TimeExample example = new TimeExample(ShareholderCoinGrantHistory.class);
        example.equals("coinTypeId", coinTypeId);
        example.equals("userId", userId);
        example.equals("type", Type.ALIVECOIN);
        example.setOrderByClause("id DESC");
        List<ShareholderCoinGrantHistory> shareholderCoinGrantHistoryList = getsByExample(example);
        if (shareholderCoinGrantHistoryList != null && shareholderCoinGrantHistoryList.size() > 7) {
            while (shareholderCoinGrantHistoryList.size() > 7) {
                ShareholderCoinGrantHistory shareholderCoinGrantHistory = shareholderCoinGrantHistoryList.get(shareholderCoinGrantHistoryList.size() - 1);
                Integer id = shareholderCoinGrantHistory.getId();
                delete(id);
                shareholderCoinGrantHistoryList.remove(shareholderCoinGrantHistory);
            }
        }
        return shareholderCoinGrantHistoryList;
    }


}
